package com.cnblogs.leizia.crm.workbench.service.impl;

import com.cnblogs.leizia.crm.utils.SqlSessionUtil;
import com.cnblogs.leizia.crm.vo.PaginationVo;
import com.cnblogs.leizia.crm.workbench.dao.ActivityRemarkDao;
import com.cnblogs.leizia.crm.workbench.domain.ActivityRemark;
import com.cnblogs.leizia.crm.workbench.service.ActivityRemarkService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author leizi
 * @create 2021-02-21 16:15
 */
public class ActivityRemarkServiceImpl implements ActivityRemarkService {
    /**
     * 根据市场活动id获取市场活动备注列表
     *
     * @param activityId 市场活动ID
     * @return
     */
    @Override
    public List<ActivityRemark> getRemarkList(String activityId) {
        ActivityRemarkDao remarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);

        List<ActivityRemark> remarkList = remarkDao.getRemarkList(activityId);

        return remarkList;
    }

    /**
     * 根据id删除备注记录
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteRemark(String id) {
        ActivityRemarkDao remarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
        int remark = remarkDao.deleteRemark(id);

        if (remark > 0) {
            return true;
        }

        return false;
    }

    /**
     * 添加备注
     *
     * @param activityRemark
     * @return
     */
    @Override
    public boolean addRemark(ActivityRemark activityRemark) {
        ActivityRemarkDao remarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
        int addResult = remarkDao.addRemark(activityRemark);

        if (addResult > 0) {
            return true;
        }

        return false;
    }

    /**
     * 根据市场活动id分页获取备注记录
     *
     * @param remarkMap
     * @return
     */
    @Override
    public PaginationVo<ActivityRemark> pageListRemark(Map<String, Object> remarkMap) {
        ActivityRemarkDao remarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
        List<ActivityRemark> remarkList = null;
        int pageNo = Integer.valueOf((String) remarkMap.get("pageNo"));
        int pageSize = Integer.valueOf((String) remarkMap.get("pageSize"));
        String activityId = (String) remarkMap.get("activityId");

        // 根据市场活动id获取总备注数
        int total = remarkDao.getTotal(activityId);

        if (total > 0) {
            // 根据市场活动id分页获取备注记录
            remarkList = remarkDao.pageListRemark(pageNo - 1, pageSize, activityId);
        }

        PaginationVo<ActivityRemark> paginationVo = new PaginationVo<>();
        paginationVo.setTotal(total);
        paginationVo.setDatalist(remarkList);

        return paginationVo;
    }

    /**
     * 修改备注
     *
     * @param remarkMap
     * @return
     */
    @Override
    public boolean editRemark(Map<String,String> remarkMap) {
        ActivityRemarkDao remarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
        int editResult = remarkDao.editRemark(remarkMap);
        if (editResult > 0) {
            return true;
        }
        return false;
    }
}
